package com.hcinfos.gsoo.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.hcinfos.gsoo.R;
import com.hcinfos.gsoo.config.Constants;
import com.hcinfos.gsoo.enity.Image;
import com.hcinfos.gsoo.util.ImageUtil;
import com.hcinfos.gsoo.util.ImageUtil.ImageCallback;

public class ShopImgListAdapter extends PagerAdapter {

	private List<Image> list;
	private Context context;
	private LayoutInflater inflater;
	private boolean isSelfData = true;

	public ShopImgListAdapter(List<Image> list, Context context) {

		this.context = context;
		this.list = list;
		inflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		((ViewPager) container).removeView((View) object);
	}

	@Override
	public Object instantiateItem(ViewGroup view, int position) {
		View imageLayout = inflater.inflate(R.layout.item_pager_image, view,
				false);
		final ImageView imageView = (ImageView) imageLayout
				.findViewById(R.id.image);
		Image img = list.get(position);
		String url = Constants.SERVER + img.getImgAddr()
				+ Constants.IMG_THUMB_512_DIR + img.getId() + ".jpg";
		if (isSelfData) {
			ImageUtil.setThumbnailView(url, imageView, context,
					new ImageCallback() {

						@Override
						public void loadImage(Bitmap bitmap, String imagePath) {
							imageView.setImageBitmap(bitmap);
						}
					}, true, false);
		} else {

			ImageUtil.setThumbnailView(img.getWebUrl(), imageView, context,
					new ImageCallback() {

						@Override
						public void loadImage(Bitmap bitmap, String imagePath) {
							imageView.setImageBitmap(bitmap);
						}
					}, true, false);
		}
		((ViewPager) view).addView(imageLayout, 0);
		return imageLayout;
	}

	@Override
	public boolean isViewFromObject(View view, Object object) {
		return view.equals(object);
	}

	@Override
	public void restoreState(Parcelable state, ClassLoader loader) {
	}

	@Override
	public Parcelable saveState() {
		return null;
	}

	@Override
	public void startUpdate(View container) {
	}

	public void setDataChange(List<Image> list) {
		this.list = list;
		notifyDataSetChanged();
	}

	public void setIsSelefData(boolean isSelfData) {
		this.isSelfData = isSelfData;
	}
}
